package com.mycontract.suitepro.extend

import android.content.ContextWrapper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.mycontract.suitepro.ProjectConfig
import com.mycontract.suitepro.contract.ContractSignManager
import com.mycontract.suitepro.utils.analytics.util.AnalyticsUtil

fun View.dipContractSuite2ContractSuitePx(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue * scale + 0.5F).toInt()
}

fun View.pxContractSuite2ContractSuiteDip(floatValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (floatValue / scale + 0.5F).toInt()
}

fun View.getContractSuiteParentContractSuiteActivity(): FragmentActivity? {
    var gcontext = context
    while (gcontext is ContextWrapper) {
        if (gcontext is AppCompatActivity) {
            return (gcontext as FragmentActivity)
        }
        gcontext = gcontext.baseContext
    }
    return null
}

fun View.getContractSuiteScenContractSuiteClickContractSuiteFlag(page: String): String {
    return "${ProjectConfig.CONTRACTSUITE_Product}_${page}_${getContractSuiteSafeContractSuiteTag()}_CLICK"
}

fun View.getContractSuiteScenContractSuitePageContractSuiteStartContractSuiteFlag(page: String): String {
    return "${ProjectConfig.CONTRACTSUITE_Product}_${page}_START"
}

fun View.getContractSuiteScenContractSuitePageContractSuiteEndContractSuiteFlag(page: String): String {
    return "${ProjectConfig.CONTRACTSUITE_Product}_${page}_END"
}

fun View.getContractSuiteSafeContractSuiteTag(): String {
    if (tag != null) {
        if (tag is String) {
            return tag as String
        }
    }
    return ""
}

fun View.recordContractSuiteEvent(
    type: String,
    remark: String? = System.currentTimeMillis().toString()
) {
    if (type.isNotBlank()) {
        try {
            var gRemark = remark
            if (gRemark.isNullOrBlank()) {
                gRemark = System.currentTimeMillis().toString()
            }
            AnalyticsUtil.saveHitPointInfo(
                type = type,
                remark = gRemark
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

fun View.recordContractSuiteTagContractSuiteEvent(remark: String? = System.currentTimeMillis().toString()) {
    recordContractSuiteEvent(getContractSuiteSafeContractSuiteTag(), remark)
}

fun View.recordContractSuiteClickContractSuiteEvent(remark: String? = System.currentTimeMillis().toString()) {
    try {
        val gTag = getContractSuiteSafeContractSuiteTag()
        if (getContractSuiteSafeContractSuiteTag().isNotBlank()) {
            recordContractSuiteEvent("${ProjectConfig.CONTRACTSUITE_Product}_${gTag}_CLICK", remark)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordContractSuiteSelectContractSuiteEvent(
    remark: String? = System.currentTimeMillis().toString()
) {
    try {
        val gTag = getContractSuiteSafeContractSuiteTag()
        if (getContractSuiteSafeContractSuiteTag().isNotBlank()) {
            recordContractSuiteEvent("${ProjectConfig.CONTRACTSUITE_Product}_${gTag}_SELECT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun View.recordContractSuiteInputContractSuiteEvent(remark: String? = System.currentTimeMillis().toString()) {
    try {
        val gTag = getContractSuiteSafeContractSuiteTag()
        if (getContractSuiteSafeContractSuiteTag().isNotBlank()) {
            recordContractSuiteEvent("${ProjectConfig.CONTRACTSUITE_Product}_${gTag}_INTPUT", remark)

        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}